package cn.wind.xboot.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;

/**
 * @author xukk
 * @date 2018/5/4.
 */
@Slf4j
public class RequestUtil {
    public static boolean hasEncryptParam(HttpServletRequest request) {
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String aa = enumeration.nextElement();
            if (aa.equals("encrypt")) {
                if (aa.equals("true"))
                    return true;
                else return false;
            }

        }
        return true;
    }

    public static boolean hasData(HttpServletRequest request) {
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            if (enumeration.nextElement().equals("data"))
                return true;
        }
        return false;
    }

    public static boolean hasEncrypt(HttpServletRequest request) {
        return hasEncryptParam(request) && hasEncryptHeader(request);
    }

    public static boolean hasEncryptHeader(HttpServletRequest request) {
        String encrypt = request.getHeader("encrypt");
        if (StringUtils.equalsIgnoreCase(encrypt, "false")) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取所有的请求头
     *
     * @param request
     * @return
     */
    public static Map<String, String> getHeaders(HttpServletRequest request) {
        Map<String, String> headerMap = new HashMap<>();
        List<String> headers = getCommonHeaders();
        headers.add("Postman-Token");
        headers.add("Proxy-Connection");
        headers.add("X-Lantern-Version");
        headers.add("Cookie");

        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            if (headers.contains(headerName)) {
                continue;
            }
            headerMap.put(headerName, request.getHeader(headerName));
        }
        return headerMap;
    }

    /**
     * 获取请求的路径参数
     *
     * @param request
     * @return
     */
    public static Map<String, String> getUriTemplateVar(HttpServletRequest request) {
        NativeWebRequest webRequest = new ServletWebRequest(request);
        Map<String, String> uriTemplateVars = (Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);

        return uriTemplateVars;
    }


    private static List<String> getCommonHeaders() {
        List<String> headers = new ArrayList<>();
        Class<HttpHeaders> clazz = HttpHeaders.class;
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getType().toString().endsWith("java.lang.String") && Modifier.isStatic(field.getModifiers())) {
                try {
                    headers.add((String) field.get(HttpHeaders.class));
                } catch (IllegalAccessException e) {
                    log.error("反射获取属性值异常-->", e);
                }
            }
        }
        return headers;
    }

    public static String getBasePath(HttpServletRequest request) {
        if (StringUtils.isNotBlank(request.getContextPath())) {
            return request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort()+ request.getContextPath();
        } else {
            return request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort();
        }
    }

}
